package graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.Vector;

import utils.Vector2d;

public class GaugeBar implements Drawable, Serializable {
	private Vector<Float> amounts;
	private Vector<Color> colors;
	
	private Vector2d coords;
	private int width, height;
	
	
	public GaugeBar(int width, int height) {
		amounts = new Vector<Float>(2);
		colors = new Vector<Color>(2);
		
		amounts.setSize(2);
		colors.setSize(2);
		amounts.set(0, 0F);
		amounts.set(1, 0F);
		colors.set(0, Color.BLACK);
		colors.set(1, Color.BLACK);
		
		coords = new Vector2d();
		this.width = width;
		this.height = height;
	}
	
	public void setNewCoords(Vector2d newCoords) {
		coords = newCoords;
	}
	
	public void setSubBarCount(int count) {
		if(count > amounts.size()) {
			int oldSize = amounts.size();
			amounts.setSize(count);
			colors.setSize(count);
			
			for(int i = oldSize; i < count; ++i) {
				amounts.set(i, 0F);
				colors.set(i, Color.BLACK);
			}
		}
		else {
			amounts.setSize(count);
			colors.setSize(count);
		}
	}
	
	public void setAmount(int index, float amount) {
		amounts.set(index, amount);
	}
	
	public void clearAmounts() {
		for(int i = 0; i < amounts.size(); ++i) {
			amounts.set(i, 0f);
		}
	}
	
	public void setColor(int index, Color color) {
		colors.set(index, color);
	}
	
	public void setColors(Vector<Color> colors) {
		this.colors = colors;
	}
	
	public void draw(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillRect((int)coords.x - 2, (int)coords.y - 2, width + 4, height + 4);
		
		g.setColor(Color.WHITE);
		g.fillRect((int)coords.x - 1, (int)coords.y - 1, width + 2, height + 2);
		
		
		g.setColor(Color.GRAY);
		g.fillRect((int)coords.x, (int)coords.y, width, height);
		
		float offset = 0;
		
		for(int i = 0; i < amounts.size(); ++i) {
			float subBarWidth = amounts.get(i) * (float)width;
			
			g.setColor(colors.get(i));
			g.fillRect(Math.round(coords.x + offset), (int)coords.y, Math.round(subBarWidth), height);
			
			offset += subBarWidth;
		}
	}
}
